<?php

namespace backend\controllers;


use backend\component\BackendBaseController;
use backend\component\ResultFormat;
use backend\service\ArticleService;
use common\models\info\Photo;
use common\models\info\Article;
use backend\service\UploadService;
use Yii;
use yii\helpers\ArrayHelper;
use common\actions\UEditorActionI;

class UploadImgController extends BackendBaseController
{
    public function init()
    {
        parent::init();
        return ArrayHelper::merge(parent::actions(), [
            "delete" => [
                'class' => "backend\actions\DeleteAction",
                'modelName' => Article::className(),
                'is_physics' => false
            ],
            "editor"=>[
                'class' => 'common\actions\UEditorActionI',
                'config' => [
                    'imageUrlPrefix' => \Yii::$app->params["attach.image.url"],
                    'imagePathFormat' =>  \Yii::$app->params["attach.image.format"],
                ]
            ],  'upload' => [
                'class' => 'common\actions\UEditorActionI',
            ],
        ]);
    }

    public function actionUp()
    {
        $upload = new UploadService();
        $uploadImage = $upload->actionMain($_FILES['file']);
        return json_encode($uploadImage);
    }

    public function actionPhoto()
    {
        $id = $_POST['id'];
        $fileList = $_POST['fileList'];
        $list = explode(',',$fileList);
        return ResultFormat::success("SUCCESS", Photo::savePhoto($list,$id));
    }
}
